﻿using MESPro.DBManager;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPro.Views
{
    /// <summary>
    /// 选择一张表,自动生成增删改查对应的存储过程以及页面视图
    /// </summary>
    public partial class AutoBuildCRUDAndView : Form
    {
        /// <summary>
        /// 选择一张表,自动生成增删改查对应的存储过程以及页面视图
        /// </summary>
        public AutoBuildCRUDAndView()
        {
            InitializeComponent();
            this.dgv_tab.AutoGenerateColumns = false;
            this.dgv_col.AutoGenerateColumns = false;
            this.Load += AutoBuildCRUDAndView_Load;
        }

        private void AutoBuildCRUDAndView_Load(object sender, EventArgs e)
        {
            this.dgv_tab.DataSource = DbContext.Db.DbMaintenance.GetTableInfoList();


        }
        /// <summary>
        /// 生成的表名
        /// </summary>
        public string TabName = "";
        /// <summary>
        /// 所有的列名
        /// </summary>
        public List<DbColumnInfo> Columns = new();
        private void dgv_tab_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                TabName = this.dgv_tab.Rows[e.RowIndex].Cells[0].Value.ToString();
                Columns = DbContext.Db.DbMaintenance.GetColumnInfosByTableName(TabName);
                this.dgv_col.DataSource = null;
                this.dgv_col.DataSource = Columns;
            }

        }

        private void dgv_col_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                if ((bool)dgv_col.Rows[e.RowIndex].Cells["Check"].EditedFormattedValue == true)
                {
                    this.dgv_col.Rows[e.RowIndex].Cells["Check"].Value = false;
                }
                else
                {
                    this.dgv_col.Rows[e.RowIndex].Cells["Check"].Value = true;
                }
            }
        }

        private void 确认生成(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
